﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RED_NET_Core.BLL;
using RED_NET_Core.Entity;
using System.Drawing;
namespace RED_NET.ContentPage
{
    public partial class InBox : System.Web.UI.Page
    {
        private static Users user;
        public static int visitId = -1;
        protected void Init()
        {

            Session["PageName"] = "Inbox";
            if (Session["USER"] == null)
            {
                Response.Redirect("~/ContentPage/Login_2.aspx");

            }
            else
            user = (Users)Session["USER"];
            if (Request.QueryString["VISIT_ID"] != null)
                visitId = Int32.Parse(Request.QueryString["VISIT_ID"]);
            else
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            if (visitId != user.UserId)
              Response.Redirect("~/ContentPage/Error/PageError.aspx");
            LinkButton_Inbox.BackColor = Color.Gray;
            LinkButton_Inbox.PostBackUrl = "~/ContentPage/News/InBox.aspx?VISIT_ID=" + visitId.ToString();
            LinkButton_Sendletter.PostBackUrl = "~/ContentPage/News/SendLetter.aspx?VISIT_ID=" + visitId.ToString();
            LinkButton_Sendbox.PostBackUrl = "~/ContentPage/News/SendBox.aspx?VISIT_ID=" + visitId.ToString();
           
            try
            {
               DataTable _dt = News_BLL.ShowRecvLetterList(user.UserId);
           
            if (_dt.Rows.Count == 0)
            {
                Label_NULL.Visible = true;
            }
            else
            {
                GridView_Letter.DataSource = _dt;
                GridView_Letter.DataBind();
            }
           }
             catch (Exception ex)
            {
                Response.Redirect("~/ContentPage/Error/PageError.aspx");
            }
        }
        protected void GridView_Letter_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_Letter.PageIndex = e.NewPageIndex;
            Init(); //重新绑定GridView数据的函数
            GridView_Letter.PageIndex = e.NewPageIndex;

        }
        protected void Page_Load(object sender, EventArgs e)
        {
                Init();
            
        }

        protected void GridView_Letter_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow )
            {
                e.Row.Cells[1].Visible = false; //如果想使第1列不可见,则将它的可见性设为false
                e.Row.Cells[2].Visible = false;

                LinkButton LinkButton_Delete = (LinkButton)e.Row.FindControl("LinkButton_Delete");
                // Set the LinkButton's CommandArgument property with the row's index.
                LinkButton_Delete.CommandArgument = e.Row.RowIndex.ToString();
          
            } 
        }

        protected void GridView_Letter_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {

                int _index = Convert.ToInt32(e.CommandArgument);
                int _Id = Int32.Parse(GridView_Letter.Rows[_index].Cells[1].Text);
                try
                {
                    News_BLL.DeleteLetter(_Id);
                }
                catch (Exception ex)
                {
                    Response.Redirect("~/ContentPage/Error/PageError.aspx");
                }
                Response.Redirect(Request.Url.ToString());
            }
         
        }
    }
}
